package com.malou.client.ventas.ingreso;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter.DateFilterMessages;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.NumericFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class IngresoPanel extends ContentPanel {
	private MalouConstants constants = GWT.create(MalouConstants.class);
	private ToolBar toolBar;
	private Button nuevoingreso;		
	public IngresoDialog dialog;
	
	public IngresoPanel(){
		Ingresos.getPaginate();
		
		final PagingToolBar toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		toolBarPag.bind(Ingresos.loader);
		
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();	
		
		columns.add(new ColumnConfig("descripcion", constants.ingresoDescripcion(), 350));
		columns.add(new ColumnConfig("monto", constants.ingresoImporte(), 80));
		columns.add(new ColumnConfig("documento", constants.ingresoDocumento(), 80));
		columns.add(new ColumnConfig("numero", constants.ingresoNumero(), 80));
		columns.add(new ColumnConfig("observaciones", constants.ingresoObservaciones(), 3500));
		
		       
		
		ColumnModel cm = new ColumnModel(columns);
		Grid<BeanModel> grid = new Grid<BeanModel>(Ingresos.store, cm);  
        grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  Ingresos.loader.load(0, Utilities.itemsPerPage);  
          }  
        });        
        
        grid.setLoadMask(true);  
        grid.setBorders(true);
        		
		setFrame(true);  
        setCollapsible(true);  
        setAnimCollapse(false);  
        
        setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.ingresoLista());  
        setLayout(new BorderLayout());
        
        toolBar = new ToolBar();
        
        nuevoingreso = new Button(constants.ingresoNuevo(), IconHelper.createPath("images/icons/addstock.png"));
        
        dialog = new IngresoDialog();
        SelectionListener<ButtonEvent> sl_new;       
        sl_new = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {	
				dialog.clearPrincipal();				
			    dialog.show();			    
			}
        };
        nuevoingreso.addSelectionListener(sl_new);
        toolBar.add(nuevoingreso);             
        
        setTopComponent(toolBar);             
        
        GridFilters filters = new GridFilters();
        filters.getMessages().setFilterText(constants.filtros());
        
        StringFilter codigoFilter = new StringFilter("codigo");
        StringFilter prendaFilter = new StringFilter("modeloprenda");
        StringFilter tiendaFilter = new StringFilter("tienda");
        StringFilter tiendaorigenFilter = new StringFilter("tiendaorigen");        
        DateFilter fechaFilter = new DateFilter("fecha");
        
        DateFilterMessages dm= new DateFilterMessages();
        dm.setAfterText(constants.despues());
        dm.setBeforeText(constants.antes());
        dm.setOnText(constants.del());
        fechaFilter.setMessages(dm);
        
        NumericFilter cantidadFilter = new NumericFilter("cantidad");
        
        filters.addFilter(codigoFilter);
        filters.addFilter(prendaFilter);
        filters.addFilter(tiendaFilter);
        filters.addFilter(tiendaorigenFilter);
        filters.addFilter(cantidadFilter);        
        filters.addFilter(fechaFilter);        
                
        grid.addPlugin(filters);     
   
        setLayout(new FitLayout());
        add(grid);
        setBottomComponent(toolBarPag);        
	}
	
	public void setPermisos(List<String> permisos){
		
		boolean add = permisos.contains("realizar");		
		
		if(!add){
			toolBar.setVisible(false);
		}		
	}
}
